/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slisfx.screens;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import slisfx.Globals;
import slisfx.exception.SLISExceptionFX;
import slisfx.resources.Load;
import slisfx.user.Userbase;
import slisfx.user.Users;
import slisfx.xml.XMLWriter;

/**
 *
 * @author Rebecca
 */
public class AddUserScreenFX extends Application implements Globals{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(final Stage primaryStage){
        try {
            primaryStage.setTitle("Add a New User to NASA SLIS.");
            primaryStage.setResizable(false);

            Image image = new Image(new Load().loadFile("Stars.png"));
            //Image image1 = new Image("src/slisfx/resources/NASA_IVV1.png");
            
            /*Creates our main Layout.
             * Sets the border to all black
             */
            BorderPane mainWindow = new BorderPane();
            mainWindow.setTop(new Rectangle(830,50, Color.BLACK));
            mainWindow.setBottom(new Rectangle(830,50, Color.BLACK));
            mainWindow.setLeft(new Rectangle(50,350, Color.BLACK));
            mainWindow.setRight(new Rectangle(50, 350, Color.BLACK));
            
            /* Main Menu 
             * Sets spacing.
             */
            VBox mainMenu = new VBox();
            mainMenu.setPadding(new Insets(10, 10, 10, 10));
            mainMenu.setSpacing(10);
            
            /* Horizantal Layout
             * Sets spacing
             */
            HBox imageBox = new HBox();
            imageBox.setPadding(new Insets(10, 10, 10, 150));
            imageBox.setSpacing(100);
            
            /* Stackpane
             * Sets background
             */
            StackPane stackPane = new StackPane();
            
            /* Title for Main Menu
             * Sets font style and size.
             */
            Text title = new Text("Please fill out the following form.");
            title.setFont(Font.font("Amble CN", FontWeight.BOLD, 14));
            title.setFill(Color.WHITE);
            mainMenu.getChildren().add(title);
            
            /* ImageView
             * Sets images
             */
            ImageView iv1BackGround = new ImageView();
            ImageView iv2 = new ImageView();
            iv1BackGround.setImage(image);
            //iv2.setImage(image1);
            
            mainMenu.getChildren().add(imageBox);
            
            /*Creating a textField*/
            final TextField uName = new TextField();
            uName.setPromptText("Enter a unique user name.");
            uName.setPrefColumnCount(10);      
            mainMenu.getChildren().add(uName);
            
            /*Creating a place to put your password*/
            final PasswordField pb = new PasswordField();
            pb.setPromptText("Enter a unique password.");
            pb.setPrefColumnCount(10);
            mainMenu.getChildren().add(pb);
            
             /*Checking the password*/
            final PasswordField pb1 = new PasswordField();
            pb1.setPromptText("Re-Enter your password.");
            pb1.setPrefColumnCount(10);
            mainMenu.getChildren().add(pb1);
            
            /*Creating a textField for permissions*/
            final TextField perm = new TextField();
            perm.setPromptText("Set the users permission");
            perm.setPrefColumnCount(10);      
            mainMenu.getChildren().add(perm);
            
            Button addButton = new Button("Add User");
            addButton.setOnAction(new EventHandler<ActionEvent>()
            {
                @Override
                public void handle(ActionEvent event) {
                    if(Userbase.getUserbase().admin_check_user(uName.getText())){
                        SLISExceptionFX except = new SLISExceptionFX("User already exists.", ADD_USER_SCREEN);
                        except.start(primaryStage);
                    }
                    else if(pb.getText().compareTo(pb1.getText()) != 0){
                        SLISExceptionFX except = new SLISExceptionFX("Passwords not Identical.", ADD_USER_SCREEN);
                        except.start(primaryStage);             
                    }
                    else{
                            Userbase.getUserbase().add_user(new Users(uName.getText(), pb.getText(), perm.getText()));
                            /*Re-Writes the xml document*/
                            new XMLWriter().writer();
                            new SystemViewFX().start(primaryStage);
                    }
                }
            });
            
            Button returnButton = new Button("Return");
            returnButton.setOnAction(new EventHandler<ActionEvent>()
            {
                @Override
                public void handle(ActionEvent event) {
                    new SystemViewFX().start(primaryStage);
                }
            });
            
            HBox buttonBox = new HBox();
            buttonBox.setPadding(new Insets(10, 10, 10, 10));
            buttonBox.setSpacing(10);
            buttonBox.getChildren().add(addButton);
            buttonBox.getChildren().add(returnButton);
            
            
            imageBox.getChildren().add(iv2);
            
            mainMenu.getChildren().add(buttonBox);
            

            /*We place it in the center of screen*/
            stackPane.getChildren().addAll(iv1BackGround, mainMenu);
            mainWindow.setCenter(stackPane);

            /*Sets the screen size and displays*/
            primaryStage.setScene(new Scene(mainWindow, WIDTH, HEIGHT));
            primaryStage.show();
        } catch (IOException ex) {
            Logger.getLogger(AddUserScreenFX.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
